package com.woniuxy.controller;

import com.woniuxy.entity.*;
import com.woniuxy.service.DoctorService;
import com.woniuxy.service.PrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class PrescriptionController {
    @Autowired
    PrescriptionService prescriptionService;
    @Autowired
    private DoctorService doctorService;

    @GetMapping("selectPreAndPatientCount/{prescriptionStatus}")
    Integer selectPreAndPatientCount(@PathVariable Integer prescriptionStatus){
        return prescriptionService.selectPreAndPatientCount(prescriptionStatus);
    }

    @PutMapping("/updateStatusByPrescriptionId/{prescriptionNo}/{prescriptionStatus}")
    public void updateStatusByPrescriptionId(@PathVariable String prescriptionNo,
                                             @PathVariable Integer prescriptionStatus){
        prescriptionService.updateStatusByPrescriptionId(prescriptionNo,prescriptionStatus);
    }

    @GetMapping("/selectPreAndPatient/{pageIndex}/{pageSize}/{prescriptionStatus}")
    public List<PrescriptionAndPatient> selectPreAndPatient(@PathVariable String pageIndex,
                                                            @PathVariable String pageSize,
                                                            @PathVariable Integer prescriptionStatus) {
        Integer index = Integer.parseInt(pageIndex);
        Integer size = Integer.parseInt(pageSize);
        List<PrescriptionAndPatient> prescriptionAndPatients =
                prescriptionService.selectPreAndPatient(index, size,prescriptionStatus);

        for (PrescriptionAndPatient prescriptionAndPatient : prescriptionAndPatients) {

            //根据医生id查询医生名和对应科室名
            Integer doctorId = prescriptionAndPatient.getDoctorId();
            DoctorAndDepartment doctorAndDepartment = doctorService.selectDoctorMessageById(doctorId);

            prescriptionAndPatient.setDoctorName(doctorAndDepartment.getDoctorName());
            prescriptionAndPatient.setDepartmentName(doctorAndDepartment.getDepartmentsName());
        }
        return prescriptionAndPatients;
    }

    /*
    * 按条件查询处方信息
    * */
    @PostMapping("/getPrescription/{pageIndex}/{pageSize}")
    public ResponseResult getPrescription(@RequestBody Prescription prescription,
                                          @PathVariable int pageIndex,
                                          @PathVariable int pageSize  ){
        PageBean<Prescription> pageBean =
                prescriptionService.getPrescriptionCondition
                        (prescription, pageIndex, pageSize);
        System.out.println(pageBean);
        return new ResponseResult(200,"success",pageBean);

    }
    /*
    * 添加基本处方信息
    * */
    @PostMapping("/addPrescription")
    public ResponseResult addPrescription(@RequestBody Prescription prescription){
        log.info("进入addPrescription");
        String uuid = prescriptionService.addPrescription(prescription);
        log.info("完成addPrescription");
        return new ResponseResult<String>(200,"success",uuid);
    }
    /*
    * 修改处方单病情描述
    * */
    @PutMapping("/prescription")
    public ResponseResult modifyConditionDetails(@RequestBody Prescription prescription){
        prescriptionService.modifyConditionDetails(prescription.getPrescriptionNo(),prescription.getConditionDetails());
        return new ResponseResult<String>(200,"success",null);
    }
    /*
     * 修改处方单状态
     * */
    @PutMapping("/modifyPrescription")
    public ResponseResult modifyPrescriptionStatus(@RequestBody Prescription prescription){
        prescriptionService.modifyPrescriptionStatus(prescription.getPrescriptionNo());
        return new ResponseResult<String>(200,"success",null);
    }

}
